<?php

if ( ! defined('EXT')) exit('Invalid file request');

/**
 * Iain WYMeditor Field Class
 *
 * @package   	Iain WYMeditor
 
 * @author    	Iain Urquhart - http://iain.co.nz
 
 * @Copyright 	(c) 2009-Present Iain Urquhart (iain.co.nz)
	
 * @Licence 	Permission is hereby granted, free of charge, to any person
				obtaining a copy of this software and associated documentation
				files (the "Software"), to deal in the Software without
				restriction, including without limitation the rights to use,
				copy, modify, merge, publish, distribute, sublicense, and/or sell
				copies of the Software, and to permit persons to whom the
				Software is furnished to do so, subject to the following
				conditions:
	
				The above copyright notice and this permission notice shall be
				included in all copies or substantial portions of the Software.
	
 * @Disclaimer 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
				EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
				OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
				NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
				HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
				WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
				FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
				OTHER DEALINGS IN THE SOFTWARE.
	
*/

$row_count = 0;
$cell_row_count = 0;

class Iain_wymeditor extends Fieldframe_Fieldtype {
 
    var $info = array(
        'name'             => 'Iain WYMeditor',
        'version'          => '0.1.1',
        'desc'             => 'Provides a WYMeditor field type',
        'docs_url'         => '',
        'versions_xml_url' => ''
    );
    
    var $default_cell_settings = array(
		'rows' => '10'
	);
	
	var $default_site_settings = array(
		'css_path' => 'extensions/fieldtypes/iain_wymeditor/wym.css'
	);

	
	/**
	 * Display Site Settings
	 */
	function display_site_settings()
	{
		global $DB, $PREFS, $DSP;

		$SD = new Fieldframe_SettingsDisplay();

		$r 	 = 	$SD->block();
		$r 	.=	$SD->row(array(
		        	$SD->label('css_path'),
		        	$SD->text('css_path', $this->site_settings['css_path'])
		            ));
		$r 	.= $SD->block_c();

		return $r;
	}

	function save_field_settings($field_settings)
	{		
		// CSS Path
		if ( ! isset($field_settings['css_path']))
		{
			$field_settings['css_path'] = $this->default_site_settings['css_path'];
		}

		
		return $field_settings;
	}

	
	function display_cell_settings($cell_settings)
	{
		global $DSP, $REGX, $FF, $LANG;

		$r = '<label class="itemWrapper">'
		   . $DSP->input_text('rows', $cell_settings['rows'], '3', '3', 'input', '30px') . NBS
		   . $LANG->line('textarea_rows')
		   . '</label>';

		return $r;
	}
 
    function display_field($field_name, $field_data)
    {
        global $DSP;
        global $row_count;
        $new_row_count = $row_count++;
        $this->include_js('wymeditor/jquery.wymeditor.js');
        $this->include_js('wymeditor/plugins/hovertools/jquery.wymeditor.hovertools.js');
		$this->include_js('wymeditor/plugins/resizable/jquery.wymeditor.resizable.js');
		
		// Get the css file location
		$css_path = isset($this->site_settings['css_path']) ? $this->site_settings['css_path'] : '';

        return '<script type="text/javascript">
				jQuery(function() {
					$(".submitBox input").addClass("wymupdate");
				    jQuery(\'.wymeditor'.$new_row_count.'\').wymeditor({
				    stylesheet: \''.$css_path.'\',
				    postInit: function(wym) {
			            wym.hovertools();
			            wym.resizable();
			        	}		
				    });
				});
				</script>'
        		. $DSP->input_textarea($field_name, $field_data, '20', 'wymeditor'.$new_row_count.'', '99%');
    }
 
 	
 	// not very efficient, but I'll sort when I figure out how...
    function display_cell($cell_name, $cell_data, $cell_settings)
    {
        global $DSP;
        global $cell_row_count;
        $new_cell_row_count = $cell_row_count++;
        $this->include_js('wymeditor/jquery.wymeditor.js');
        $this->include_js('wymeditor/plugins/hovertools/jquery.wymeditor.hovertools.js');
		$this->include_js('wymeditor/plugins/resizable/jquery.wymeditor.resizable.js');
		
		// Get the css file location
		$css_path = isset($this->site_settings['css_path']) ? $this->site_settings['css_path'] : '';
		
        return '<script type="text/javascript">
				jQuery(function() {
					$(".submitBox input").addClass("wymupdate");
				    jQuery(\'.wymeditor-cell'.$new_cell_row_count.'\').wymeditor({	
					stylesheet: \''.$css_path.'\',
				    skin: \'compact\',					
				    postInit: function(wym) {
			            wym.hovertools();
			            wym.resizable();
			        	}		
				    });
				});
				</script>'
        	. $DSP->input_textarea($cell_name, $cell_data, $cell_settings['rows'], 'wymeditor-cell'.$new_cell_row_count.'', '99%');
        
    }
 
}

?>